package com.guli.eduucenter.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.MD5;
import com.guli.eduucenter.bean.UcenterMember;
import com.guli.eduucenter.bean.vo.LoginVo;
import com.guli.eduucenter.bean.vo.RegisterVo;
import com.guli.eduucenter.mapper.UcenterMemberMapper;
import com.guli.eduucenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.msmservice.bean.EmailCode;
import com.guli.msmservice.service.EmailCodeService;
import com.guli.msmservice.utils.Msm_Email_Util;
import com.guli.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.ApiModelProperty;
import jdk.internal.org.objectweb.asm.tree.InnerClassNode;
import org.apache.poi.sl.draw.geom.Guide;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-04-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
//
//    @Resource
//    private RedisTemplate<String,String> redisTemplate;

    @Resource
    private EmailCodeService emailCodeService;

    @Override
    @ApiModelProperty("会员登录")
    public String login(LoginVo loginVo) {
        //1.判断手机号和密码是否为空
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }
        //2.从数据库查询用户是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        if(ucenterMember == null){
            throw new GuliException(20001,"用户还未注册，请先注册");
        }
        //3.校验密码
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        //4.判断账号是否封禁
        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"账号已被封禁");
        }
        //5.生产token
        String jwtToken = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String code = registerVo.getCode().toLowerCase();
        String password = registerVo.getPassword();
        System.out.println(registerVo);
        //1.判断手机号和密码是否为空
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) ||
        StringUtils.isEmpty(nickname) || StringUtils.isEmpty(code)){
            throw new GuliException(20001,"注册失败");
        }

        //判断验证码是否正确
        String redisCode;
        //redisCode = redisTemplate.opsForValue().get(mobile); //从redis中取出判断
        //从mysql中取出查看
        QueryWrapper<EmailCode> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("mobile",mobile);
        EmailCode one = emailCodeService.getOne(wrapper1);
        if(one == null){
            throw new GuliException(20001,"验证码错误");
        }
        redisCode = one.getCode(); //redis太卡了就不用redis了
        System.out.println(redisCode+"________________验证验证码______________________________");
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }

        //判断用户是否已经注册
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(wrapper);
        if(integer > 0){
            throw new GuliException(20001,"用户已经注册，请登录");
        }
        //添加到数据库
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));//密码加密
        ucenterMember.setAvatar("https://yinliangtao-edu.oss-cn-beijing.aliyuncs.com/2022/04/02/41fb711b55094cb4b8b4b9dd2ebb9df6_file.png");
        baseMapper.insert(ucenterMember);
    }

    //判断微信用户是否已经注册在数据库中
    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
        return ucenterMember;
    }

    @Override
    public Integer getRegisterCountByDate(String day) {
        Integer count = baseMapper.getRegisterCountByDate(day);
        return count;
    }
}
